package com.thenie.chat.validator;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @Description 参数解析器
 * @Author thenie
 * @Data 2022/5/19 15:30
 */
@Slf4j
public class CurrentUserArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        //如果参数中存在注解CurrentUser
        return parameter.hasParameterAnnotation(CurrentUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer modelAndView, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        //进行
        Object currentUserInfo = webRequest.getAttribute("currentUser", RequestAttributes.SCOPE_REQUEST);
        if(null == currentUserInfo)
            log.warn("---------------用户未登录---------------");
        return currentUserInfo;
    }
}
